package cn.csc.module.shop.service.businessdic;

import cn.csc.framework.common.pojo.PageResult;
import cn.csc.module.shop.controller.admin.businessdic.vo.ShopBusinessDicCreateReqVO;
import cn.csc.module.shop.controller.admin.businessdic.vo.ShopBusinessDicExportReqVO;
import cn.csc.module.shop.controller.admin.businessdic.vo.ShopBusinessDicPageReqVO;
import cn.csc.module.shop.controller.admin.businessdic.vo.ShopBusinessDicUpdateReqVO;
import cn.csc.module.shop.convert.businessdic.ShopBusinessDicConvert;
import cn.csc.module.shop.dal.dataobject.businessdic.ShopBusinessDicDO;
import cn.csc.module.shop.dal.mysql.businessdic.ShopBusinessDicMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.csc.module.shop.enums.ErrorCodeConstants.BUSINESS_DIC_NOT_EXISTS;

/**
 * 企业经营范围字典数据 Service 实现类
 *
 * @author liuchuang
 */
@Service
@Validated
public class ShopBusinessDicServiceImpl implements ShopBusinessDicService {

    @Resource
    private ShopBusinessDicMapper businessDicMapper;

    @Override
    public BigInteger createBusinessDic(ShopBusinessDicCreateReqVO createReqVO) {
        // 插入
        ShopBusinessDicDO businessDic = ShopBusinessDicConvert.INSTANCE.convert(createReqVO);
        businessDicMapper.insert(businessDic);
        // 返回
        return businessDic.getId();
    }

    @Override
    public void updateBusinessDic(ShopBusinessDicUpdateReqVO updateReqVO) {
        // 校验存在
        validateBusinessDicExists(updateReqVO.getId());
        // 更新
        ShopBusinessDicDO updateObj = ShopBusinessDicConvert.INSTANCE.convert(updateReqVO);
        businessDicMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessDic(BigInteger id) {
        // 校验存在
        validateBusinessDicExists(id);
        // 删除
        businessDicMapper.deleteById(id);
    }

    private void validateBusinessDicExists(BigInteger id) {
        if (businessDicMapper.selectById(id) == null) {
            throw exception(BUSINESS_DIC_NOT_EXISTS);
        }
    }

    @Override
    public ShopBusinessDicDO getBusinessDic(BigInteger id) {
        return businessDicMapper.selectById(id);
    }

    @Override
    public List<ShopBusinessDicDO> getBusinessDicList(Collection<BigInteger> ids) {
        return businessDicMapper.selectBatchIds(ids);
    }

    @Override
    public List<ShopBusinessDicDO> getBusinessDicListOfAll() {
        return businessDicMapper.selectList();
    }

    @Override
    public PageResult<ShopBusinessDicDO> getBusinessDicPage(ShopBusinessDicPageReqVO pageReqVO) {
        return businessDicMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ShopBusinessDicDO> getBusinessDicList(ShopBusinessDicExportReqVO exportReqVO) {
        return businessDicMapper.selectList(exportReqVO);
    }

}
